home *** CD-ROM | disk | FTP | other *** search
/ Aminet 2 / Aminet AMIGA CDROM (1994)(Walnut Creek)[Feb 1994][W.O. 44790-1].iso / Aminet / gfx / misc / phoon.lha / xphoon.c < prev   
C/C++ Source or Header  |  1992-09-14  |  9KB  |  288 lines

  1. /*
  2. ** Copyright (C) 1988 by Jef Poskanzer and Craig Leres.
  3. **
  4. ** Permission to use, copy, modify, and distribute this software and its
  5. ** documentation for any purpose and without fee is hereby granted, provided
  6. ** that the above copyright notice appear in all copies and that both that
  7. ** copyright notice and this permission notice appear in supporting
  8. ** documentation.  This software is provided "as is" without express or
  9. ** implied warranty.
  10.  
  11.  *  Last modified: Mon Sep 14 01:33:35 1992 too
  12.  *
  13.  */
  14.  
  15. #include <math.h>
  16. #include "amiga.h"
  17. #include "original/tws.h"
  18.  
  19. #ifndef NULL
  20. #define NULL 0
  21. #endif
  22.  
  23. char *argv0;
  24. int shadeflag = 0;
  25. int demoflag = 0;
  26. int reverseflag = 0;
  27. int doubleflag = 0;
  28.  
  29. void    checkbitmapsize(int *, int *, int, int, int *, int *, int *, int *);
  30. void    getbitmap(int, int, int, int, char **, int *, int *, int *, int *);
  31.  
  32. main(argc, argv)
  33.     int argc;
  34.     char **argv;
  35. {
  36.     int w, h, fm_w, fm_h, cx = 0, cy = 0, rx = 0, ry = 0;
  37.     char *bits, *origbits;
  38.     char *filename = NULL;
  39.     int i, value;
  40.  
  41.     argv0 = argv[0];
  42.  
  43. again:
  44.     argv++;
  45.     argc--;
  46.     if (argc > 0 && strcmp(argv[0], "-d") == 0) {
  47.         doubleflag = 1;
  48.         goto again;
  49.     }
  50.     if (argc > 0 && strcmp(argv[0], "-f") == 0) {
  51.         if ((filename = argv[1]) == NULL)
  52.           goto usage;
  53.         argv++;
  54.         argc--;
  55.         goto again;
  56.     }
  57.     if (argc > 0 && strcmp(argv[0], "-g") == 0) {
  58.         argv++;
  59.         argc--;
  60.         for (i = 0; i < 4; i++) {
  61.           value = (int)argv[0][2 * i];
  62.           switch (value) {
  63.           case '\0':    goto again;
  64.           case '-':     value = -1;     break;
  65.           case '+':     value =  1;     break;
  66.           default:    goto usage;
  67.           }
  68.           if (!isdigit(argv[0][2 * i + 1]))
  69.             goto usage;
  70.           value = value * (argv[0][2 * i + 1] - '0');
  71.           switch (i) {
  72.           case 0:    cx = value;    break;
  73.           case 1:    cy = value;    break;
  74.           case 2:    rx = value;    break;
  75.           case 3:    ry = value;    break;
  76.           }
  77.         }
  78.         goto again;
  79.     }
  80.     if (argc > 0 && strcmp(argv[0], "-r") == 0) {
  81.         reverseflag = 1;
  82.         goto again;
  83.     }
  84.     if (argc > 0 && strcmp(argv[0], "-s") == 0) {
  85.         shadeflag = 1;
  86.         goto again;
  87.     }
  88.     if (argc > 0) {
  89. usage:
  90.         write(2, "usage: phoon [-d] [-s] [-f filename] [-g dcx[dcy[drx[dry]]]] [-r]\n", 66);
  91.         exit(0);
  92.  
  93.     }
  94.     write(1, "Phase of the moon, Amiga version 1.3 (14.9.1992)\n",50);
  95.     am_OpenLibraries();
  96.  
  97.     am_GetScreenSize(&w, &h);
  98.     bits = am_LoadFullmoon(&fm_w, &fm_h, filename);
  99.     origbits = bits;
  100.     checkbitmapsize(&w, &h, fm_w, fm_h, &cx, &cy, &rx, &ry);
  101.  
  102.     getbitmap(w, h, fm_w, fm_h, &bits, &cx, &cy, &rx, &ry);
  103.     hackbits(dtwstime(), w, h, bits, cx, cy, rx, ry);
  104.     am_MakeIFFile(w, h, bits, reverseflag);
  105.     am_CloseLibraries();
  106.     free(origbits);
  107.     return 0;
  108. }
  109.  
  110. short leftmask[16] = {
  111.     0x8000, 0xc000, 0xe000, 0xf000, 0xf800, 0xfc00, 0xfe00, 0xff00,
  112.     0xff80, 0xffc0, 0xffe0, 0xfff0, 0xfff8, 0xfffc, 0xfffe, 0xffff,
  113. };
  114. short rightmask[16] = {
  115.     0x7fff, 0x3fff, 0x1fff, 0x0fff, 0x07ff, 0x03ff, 0x01ff, 0x00ff,
  116.     0x007f, 0x003f, 0x000f, 0x001f, 0x0007, 0x0003, 0x0001, 0x0000,
  117. };
  118. #ifdef notdef
  119. static char  leftmask[8] = {0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01, 0x00};
  120. static char rightmask[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
  121. /*
  122. static char  leftmask[8] = {0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80};
  123. static char rightmask[8] = {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f};
  124. */
  125. #endif notdef
  126.  
  127. static short  shades_0_bits[] = {0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
  128.                   0x0000,0x0000};
  129. static short  shades_1_bits[] = {0x0101,0x0000,0x0000,0x0000,0x0000,0x0000,
  130.                   0x0000,0x0000};
  131. static short  shades_2_bits[] = {0x0101,0x0000,0x0404,0x0000,0x0000,0x0000,
  132.                   0x0000,0x0000};
  133. static short  shades_3_bits[] = {0x0101,0x0000,0x0404,0x0000,0x8080,0x0000,
  134.                   0x0000,0x0000};
  135. static short  shades_4_bits[] = {0x0101,0x0000,0x0404,0x0000,0x8080,0x0000,
  136.                   0x0000,0x1010};
  137. static short  shades_5_bits[] = {0x0101,0x4040,0x0404,0x0000,0x8080,0x0000,
  138.                   0x0000,0x1010};
  139. static short  shades_6_bits[] = {0x0101,0x4040,0x0404,0x2020,0x8080,0x0000,
  140.                   0x0000,0x1010};
  141. static short  shades_7_bits[] = {0x0101,0x4040,0x0404,0x2020,0x8080,0x0000,
  142.                   0x0101,0x1010};
  143. static short  shades_8_bits[] = {0x0101,0x4040,0x0404,0x2020,0x8080,0x2020,
  144.                   0x0101,0x1010};
  145. static short  shades_9_bits[] = {0x0101,0x4040,0x0404,0x2020,0x8282,0x2020,
  146.                   0x0101,0x1010};
  147. static short shades_10_bits[] = {0x0101,0x4040,0x0404,0x2020,0x8282,0x2020,
  148.                   0x0505,0x1010};
  149. static short shades_11_bits[] = {0x0101,0x4040,0x0404,0x2020,0x8282,0x2020,
  150.                   0x0505,0x5050};
  151. static short shades_12_bits[] = {0x0101,0x4040,0x0505,0x2020,0x8282,0x2020,
  152.                   0x0505,0x5050};
  153. static short shades_13_bits[] = {0x0101,0x5050,0x0505,0x2020,0x8282,0x2020,
  154.                   0x0505,0x5050};
  155. static short shades_14_bits[] = {0x0101,0x5050,0x0505,0x2020,0x7575,0x2020,
  156.                   0x0505,0x5050};
  157. static short shades_15_bits[] = {0x0505,0x5050,0x0505,0x2020,0x7575,0x2020,
  158.                   0x0505,0x5050};
  159.  
  160. short *shades_s[16] = {
  161.     shades_0_bits,  shades_1_bits,  shades_2_bits,  shades_3_bits,
  162.     shades_4_bits,  shades_5_bits,  shades_6_bits,  shades_7_bits,
  163.     shades_8_bits,  shades_9_bits,  shades_10_bits, shades_11_bits,
  164.     shades_12_bits, shades_13_bits, shades_14_bits, shades_15_bits };
  165.  
  166.  
  167. static short  shaded_0_bits[] = {0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
  168.                   0x0000,0x0000};
  169. static short  shaded_1_bits[] = {0x0003,0x0000,0x0000,0x0000,0x0000,0x0000,
  170.                   0x0000,0x0000};
  171. static short  shaded_2_bits[] = {0x0003,0x0000,0x0030,0x0000,0x0000,0x0000,
  172.                   0x0000,0x0000};
  173. static short  shaded_3_bits[] = {0x0003,0x0000,0x0030,0x0000,0xc000,0x0000,
  174.                   0x0000,0x0000};
  175. static short  shaded_4_bits[] = {0x0003,0x0000,0x0030,0x0000,0xc000,0x0000,
  176.                   0x0000,0x0300};
  177. static short  shaded_5_bits[] = {0x0003,0x3000,0x0030,0x0000,0xc000,0x0000,
  178.                   0x0000,0x0300};
  179. static short  shaded_6_bits[] = {0x0003,0x3000,0x0030,0x0c00,0xc000,0x0000,
  180.                   0x0000,0x0300};
  181. static short  shaded_7_bits[] = {0x0003,0x3000,0x0030,0x0c00,0xc000,0x0000,
  182.                   0x0003,0x0300};
  183. static short  shaded_8_bits[] = {0x0003,0x3000,0x0030,0x0c00,0xc000,0x0c00,
  184.                   0x0003,0x0300};
  185. static short  shaded_9_bits[] = {0x0003,0x3000,0x0030,0x0c00,0xc00c,0x0c00,
  186.                   0x0003,0x0300};
  187. static short shaded_10_bits[] = {0x0003,0x3000,0x0030,0x0c00,0xc00c,0x0c00,
  188.                   0x0033,0x0300};
  189. static short shaded_11_bits[] = {0x0003,0x3000,0x0030,0x0c00,0xc00c,0x0c00,
  190.                   0x0033,0x3300};
  191. static short shaded_12_bits[] = {0x0003,0x3000,0x0033,0x0c00,0xc00c,0x0c00,
  192.                   0x0033,0x3300};
  193. static short shaded_13_bits[] = {0x0003,0x3300,0x0033,0x0c00,0xc00c,0x0c00,
  194.                   0x0033,0x3300};
  195. static short shaded_14_bits[] = {0x0003,0x3300,0x0033,0x0c00,0x3f33,0x0c00,
  196.                   0x0033,0x3300};
  197. static short shaded_15_bits[] = {0x0033,0x3300,0x0033,0x0c00,0x3f33,0x0c00,
  198.                   0x0033,0x3300};
  199.  
  200. short *shades_d[16] = {
  201.     shaded_0_bits,  shaded_1_bits,  shaded_2_bits,  shaded_3_bits,
  202.     shaded_4_bits,  shaded_5_bits,  shaded_6_bits,  shaded_7_bits,
  203.     shaded_8_bits,  shaded_9_bits,  shaded_10_bits, shaded_11_bits,
  204.     shaded_12_bits, shaded_13_bits, shaded_14_bits, shaded_15_bits };
  205.  
  206. #ifdef PI
  207. #undef PI
  208. #endif
  209. #define PI 3.14159265358979323846  /* Assume not near black hole or in
  210.                       Tennessee */
  211.  
  212. double jtime(), phase();
  213.  
  214. hackbits(t, w, h, bits, cx, cy, rx, ry)
  215.     struct tws *t;
  216.     int w, h;
  217.     char *bits;
  218.     int cx, cy, rx, ry;
  219. {
  220.     short *plane;
  221.     double jd, angphase, cphase, aom, cdist, cangdia, csund, csuang;
  222.     int i;
  223.     register int x, y;
  224.     int xleft, xright;
  225.     double fxleft, fxright;
  226.     double fy;
  227.     int wxright, bxright, wxleft, bxleft;
  228.     int off;
  229.     double cap, ratio;
  230.     int shadeindex;
  231.     short shade, **shades;
  232.     static double demoinc = 0.0;
  233.  
  234.     jd = jtime( t );
  235.  
  236.     angphase = phase( jd, &cphase, &aom, &cdist, &cangdia, &csund, &csuang);
  237.     cap = cos( angphase );
  238.     /* Hack to figure approximate earthlighting. */
  239.     if ( cphase < 0.1 ) cphase = 0.1;
  240.     if ( cphase > 0.9 ) cphase = 0.9;
  241.     ratio = (1.0 - cphase) / cphase;  /* ratio varies from 9.0 to 0.111 */
  242.     shadeindex = (int) ( ratio / 9.0 * 15.9999 );
  243.  
  244. #ifdef DEBUG
  245. printf("angphase %f, cap %f\n", angphase, cap);
  246. #endif
  247.     shade = 0x0000;
  248.     shades = doubleflag? shades_d: shades_s;
  249.  
  250.     plane = (short *)bits;
  251.  
  252.     for (i = 0; i < 2 * ry; i++) {
  253.         y = cy - ry + i;
  254.         fy = i - ry;
  255.         fxright = rx * sqrt(1.0 - (fy * fy) / (ry * ry));
  256.         fxleft = - fxright;
  257.         if (angphase >= 0.0 && angphase < PI)
  258.             fxright *= cap;
  259.         else
  260.             fxleft *= cap;
  261.  
  262.         xright = fxright + cx;
  263.         xleft = fxleft + cx;
  264.  
  265.         wxright = xright / 16;
  266.         bxright = xright % 16;
  267.  
  268.         wxleft = xleft / 16;
  269.         bxleft = xleft % 16;
  270.  
  271.         off = y * ((w + 15) / 16);
  272.  
  273.         if ( shadeflag )
  274.             shade = shades[shadeindex][y % 8];
  275.  
  276.         if (wxleft == wxright)
  277.             plane[wxleft + off] &=
  278.                 leftmask[bxleft] | shade | rightmask[bxright];
  279.         else {
  280.             plane[wxleft + off] &= leftmask[bxleft] | shade;
  281.             for (x = wxleft + 1; x < wxright; x++)
  282.                 plane[x + off] &= shade;
  283.             plane[wxright + off] &= rightmask[bxright] | shade;
  284.         }
  285.     }
  286. }
  287.  
  288.